/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agendacrud;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Ana Carolina, Beatriz Henrique, Patricia Tertuliano, Weslly Akatuka
 */
public class Conexao {


public Conexao () {    }    


public static Connection connect = null;


    public static void Conectar() {
        System.out.println("Conectando ao banco...");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/agenda", "root", "");
            //connect = DriverManager.getConnection("jdbc:mysql://localhost:3307/agenda", "root", "admin");
            System.out.println("Conectado! :)");
            
//            String query = "INSERT INTO agenda (nome, tel) VALUES ('rafael', '9999')";
//            Statement statement = connect.createStatement();
//            statement.executeUpdate(query);
            
            
//            ResultSet resultado;
//            resultado = statement.executeQuery("SELECT * FROM agenda");
//            
//            while ( resultado.next() ) {
//            
//                System.out.println(resultado.getString("nome"));
//            }
    
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            System.out.println(e);
            throw new RuntimeException(e);
        }


    }
    
    public static void inserir(String nome, String tel){
        String query = "INSERT INTO agenda (nome, tel) VALUES ('" + nome + "', '" + tel + "')";
            Statement statement = null;
        try {
            statement = connect.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            statement.executeUpdate(query);
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static ResultSet buscar(String busca){
        Statement statement = null;
        try {
            statement = connect.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
            ResultSet resultado = null;
        try {
            resultado = statement.executeQuery("SELECT * FROM agenda WHERE ((nome LIKE '%" + busca + "%') OR (tel LIKE '%" + busca + "%'))");
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return resultado;
        
    }
}